package at.ac.univie.zsu.aguataplan.init;

import java.util.Date;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import at.ac.univie.zsu.aguataplan.domain.Club;
import at.ac.univie.zsu.aguataplan.domain.Event;
import at.ac.univie.zsu.aguataplan.domain.EventAttribute;
import at.ac.univie.zsu.aguataplan.domain.EventAttributeValueString;
import at.ac.univie.zsu.aguataplan.domain.Team;
import at.ac.univie.zsu.aguataplan.exception.DatabaseException;
import at.ac.univie.zsu.aguataplan.exception.LogicException;
import at.ac.univie.zsu.aguataplan.service.ClubManager;
import at.ac.univie.zsu.aguataplan.service.DetectedEventManager;
import at.ac.univie.zsu.aguataplan.service.EventManager;
import at.ac.univie.zsu.aguataplan.service.GameManager;
import at.ac.univie.zsu.aguataplan.service.TeamManager;
import at.ac.univie.zsu.aguataplan.util.InitLogger;
import at.ac.univie.zsu.aguataplan.video.VideoService;

/**
 * @author gerry
 * 
 */
public class Init {

	private static ApplicationContext context;

	private VideoService videoService;
	private ClubManager clubManager;
	private EventManager eventManager;
	private TeamManager teamManager;
	private GameManager gameManager;
	private DetectedEventManager detectedEventManager;

	// -------------

	// event
	private Event event1;
	private Event event2;
	private Event event3;

	private EventAttribute eventAttribute1;
	private EventAttribute eventAttribute2;
	private EventAttribute eventAttribute3;

	private EventAttributeValueString eventAttributeValueString1;
	private EventAttributeValueString eventAttributeValueString2;
	private EventAttributeValueString eventAttributeValueString3;

	public static final String EVENT_1_NAME = "ev1";
	public static final String EVENT_1_DESCRIPTION = "desc1";
	public static final String EVENT_1_SHORTCUT = "A";
	public static final String EVENT_2_NAME = "ev2";
	public static final String EVENT_2_DESCRIPTION = "desc2";
	public static final String EVENT_2_SHORTCUT = "B";
	public static final String EVENT_3_NAME = "ev3";
	public static final String EVENT_3_DESCRIPTION = "desc3";
	public static final String EVENT_3_SHORTCUT = "C";

	public static final String EVENTATTRIBUTE_1_NAME = "eventAttribute1";
	public static final String EVENTATTRIBUTE_1_SHORTCUT = "A";
	public static final String EVENTATTRIBUTE_2_NAME = "eventAttribute2";
	public static final String EVENTATTRIBUTE_2_SHORTCUT = "B";
	public static final String EVENTATTRIBUTE_3_NAME = "eventAttribute3";
	public static final String EVENTATTRIBUTE_3_SHORTCUT = "C";

	public static final String EVENTATTRIBUTEVALUESTRING_1_VALUE = "eventAttributeValueString1";
	public static final String EVENTATTRIBUTEVALUESTRING_2_VALUE = "eventAttributeValueString2";
	public static final String EVENTATTRIBUTEVALUESTRING_3_VALUE = "eventAttributeValueString3";

	// club
	private Club club1;
	private Club club2;
	private Club club3;
	public static final String CLUB_1_NAME = "club1";
	public static final String CLUB_2_NAME = "club2";
	public static final String CLUB_3_NAME = "club3";

	// team
	private Team team1;
	private Team team2;
	private Team team3;
	public static final String TEAM_1_NAME = "team1";
	public static final String TEAM_2_NAME = "team2";
	public static final String TEAM_3_NAME = "team3";

	// tournament
	public static final String GAME_1_DESC = "game1Desc";
	public static final String GAME_1_RESULT = "game1Result";
	public static final String GAME_2_DESC = "game2Desc";
	public static final String GAME_2_RESULT = "game2Result";

	// ----------------------------------------------------
	// MAIN
	// ----------------------------------------------------

	/**
	 * @param args
	 * @throws DatabaseException
	 * @throws LogicException
	 */
	public static void main(String[] args) throws DatabaseException, LogicException {
		InitLogger.initLogger();

		context = new ClassPathXmlApplicationContext("META-INF/spring/applicationContextInit.xml");
		Init init = new Init();
		init.cleanAguataplan();
		init.prepareAguataplan();
	}

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public Init() {
		this.videoService = context.getBean(VideoService.class);
		this.clubManager = context.getBean(ClubManager.class);
		this.eventManager = context.getBean(EventManager.class);
		this.teamManager = context.getBean(TeamManager.class);
		this.gameManager = context.getBean(GameManager.class);
		this.detectedEventManager = context.getBean(DetectedEventManager.class);
	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	/**
	 * clean up database
	 * 
	 * @throws DatabaseException
	 */
	private void cleanAguataplan() throws DatabaseException {
		detectedEventManager.cleanDetectedEvent();
		gameManager.cleanGame();
		eventManager.cleanEventEventAttributeEventAttributeValueString();
		clubManager.cleanClub();
		teamManager.cleanTeam();
	}

	/**
	 * fill database with useful data
	 * 
	 * @throws DatabaseException
	 * @throws LogicException
	 */
	private void prepareAguataplan() throws DatabaseException, LogicException {
		// event
		event1 = eventManager.addEvent(EVENT_1_NAME, EVENT_1_DESCRIPTION, EVENT_1_SHORTCUT);
		event2 = eventManager.addEvent(EVENT_2_NAME, EVENT_2_DESCRIPTION, EVENT_2_SHORTCUT);
		event3 = eventManager.addEvent(EVENT_3_NAME, EVENT_3_DESCRIPTION, EVENT_3_SHORTCUT);

		eventAttribute1 = eventManager.addEventAttribute(EVENTATTRIBUTE_1_NAME, EVENTATTRIBUTE_1_SHORTCUT);
		eventAttribute2 = eventManager.addEventAttribute(EVENTATTRIBUTE_2_NAME, EVENTATTRIBUTE_2_SHORTCUT);
		eventAttribute3 = eventManager.addEventAttribute(EVENTATTRIBUTE_3_NAME, EVENTATTRIBUTE_3_SHORTCUT);

		eventAttributeValueString1 = eventManager.addEventAttributeValueString(EVENTATTRIBUTEVALUESTRING_1_VALUE);
		eventAttributeValueString2 = eventManager.addEventAttributeValueString(EVENTATTRIBUTEVALUESTRING_2_VALUE);
		eventAttributeValueString3 = eventManager.addEventAttributeValueString(EVENTATTRIBUTEVALUESTRING_3_VALUE);

		// event relation
		initEvent();
		eventManager.addEventAttributeToEvent(event1, eventAttribute1);
		initEvent();
		eventManager.addEventAttributeToEvent(event1, eventAttribute2);
		initEvent();
		eventManager.addEventAttributeToEvent(event2, eventAttribute2);
		initEvent();
		eventManager.addEventAttributeToEvent(event2, eventAttribute3);

		initEvent();
		eventManager.addEventAttributeValueStringtoEventAttribute(eventAttribute1, eventAttributeValueString1);
		initEvent();
		eventManager.addEventAttributeValueStringtoEventAttribute(eventAttribute1, eventAttributeValueString2);
		initEvent();
		eventManager.addEventAttributeValueStringtoEventAttribute(eventAttribute2, eventAttributeValueString2);
		initEvent();
		eventManager.addEventAttributeValueStringtoEventAttribute(eventAttribute2, eventAttributeValueString3);

		// club
		club1 = clubManager.addClub(CLUB_1_NAME);
		club2 = clubManager.addClub(CLUB_2_NAME);
		club3 = clubManager.addClub(CLUB_3_NAME);

		// team
		team1 = teamManager.addTeam(TEAM_1_NAME, club1);
		team2 = teamManager.addTeam(TEAM_2_NAME, club2);
		team3 = teamManager.addTeam(TEAM_3_NAME, club3);

		// tournament
		initTeam();
		gameManager.addGame(GAME_1_DESC, team1, team2, new Date(), GAME_1_RESULT);
		initTeam();
		gameManager.addGame(GAME_2_DESC, team2, team3, new Date(), GAME_2_RESULT);

	}

	/**
	 * reinitialize events after updating - be convenient and reinitialize all
	 * of them...
	 * 
	 * @throws DatabaseException
	 */
	private void initEvent() throws DatabaseException {
		event1 = eventManager.getEventById(event1.getId());
		event2 = eventManager.getEventById(event2.getId());
		event3 = eventManager.getEventById(event3.getId());

		eventAttribute1 = eventManager.getEventAttributeById(eventAttribute1.getId());
		eventAttribute2 = eventManager.getEventAttributeById(eventAttribute2.getId());
		eventAttribute3 = eventManager.getEventAttributeById(eventAttribute3.getId());

		eventAttributeValueString1 = eventManager.getEventAttributeValueStringById(eventAttributeValueString1.getId());
		eventAttributeValueString2 = eventManager.getEventAttributeValueStringById(eventAttributeValueString2.getId());
		eventAttributeValueString3 = eventManager.getEventAttributeValueStringById(eventAttributeValueString3.getId());

	}

	private void initTeam() throws DatabaseException {
		team1 = teamManager.getTeamById(team1.getId());
		team2 = teamManager.getTeamById(team2.getId());
		team3 = teamManager.getTeamById(team3.getId());
	}

}
